<?php
/**
 * @version      BOSSCMS-Free
 * @trademark    BOSSCMS
 * @copyright    Copyright © by BOSSCMS-Free (https://www.bosscms.net/) 保留所有权利
 * @license      https://gitee.com/Greenpeas/BOSSCMS/license/ 和 https://www.bosscms.net/freelicense/
 * @link         https://www.bosscms.net/
 * @author       温州软界网络技术有限公司
**/
defined('IS_OK') or exit('Access Forbidden');

into::basic_class('admin');

class safe extends admin
{
	/* BOSS_CMS */
	public function init()
	{
		global $G;
		$G['cover'] = $this->cover();
		if(preg_match("/^\w+$/", $old=arrExist($G['get'],'old_folder'))){
			if($G['path']['folder']!=$old && is_file(ROOT_PATH.$old.'/index.php')){
				$str = file_get_contents(ROOT_PATH.$old.'/index.php');
				$res = dir::read(ROOT_PATH.$old.'/');
				if(strstr($str,"define('IS_INSIDE',true);") && count($res['file'])==1){
					dir::remove(ROOT_PATH.$old.'/');
				}
			}
		}
		echo $this->theme('safe/safe');
	}
	
	public function add()
	{
		global $G;
		$this->cover('safe','M');
		if(isset($G['post'])){
			if(!$G['post']['admin_folder']){
				alert('后台文件夹不能为空！');
			}
			$BOSSCMS;
			$data = array(
				'admin_login_captcha'  => $G['post']['admin_login_captcha'],
				'upload_maxsize'       => $G['post']['upload_maxsize'],
				'upload_extension'     => preg_replace('/\\\"(\w)/','\".$1',$G['post']['upload_extension']),
				'ueditor_catchimage'   => $G['post']['ueditor_catchimage']
			);
			foreach($data as $k=>$v){
				mysql::select_set(array('name'=>$k,'value'=>$v,'parent'=>'0','type'=>$k=='admin_login_captcha'?'1':'0','lang'=>'0'),'config',array('value'));
			}
						
			if($G['path']['folder'] != $G['post']['admin_folder']){
				if(!preg_match("/^\w+$/", $G['post']['admin_folder'])){
					alert('文件夹名称必须为英文、数字、下划线等字符');
				}
				if(is_dir(ROOT_PATH.$G['post']['admin_folder'].'/')){
					alert('文件夹已存在');
				}
				$res = dir::read(ROOT_PATH);
				foreach($res['dir'] as $dir){
					if($dir!=$G['path']['folder'] && is_file(ROOT_PATH.$dir.'/index.php')){
						if(strstr(file_get_contents(ROOT_PATH.$dir.'/index.php'),"define('IS_INSIDE',true);")){
							$r = dir::read(ROOT_PATH.$dir.'/');
							if(count($r['file'])==1){
								dir::remove(ROOT_PATH.$dir.'/');
							}
						}
					}
				}
				dir::copydir(ROOT_PATH.$G['path']['folder'].'/', ROOT_PATH.$G['post']['admin_folder'].'/');
				alert('操作成功', '../'.$G['post']['admin_folder'].'/'.url::mpf('safe','safe','init',array('admin_folder'=>$G['post']['admin_folder'],'old_folder'=>$G['path']['folder'])));
			}else{
				alert('操作成功', url::mpf('safe','safe','init'));
			}
		}
	}
}
?>